package fun.symtable;

import fun.parser.EvaluationException;
import fun.parser.ast.node.base.Expression;
import fun.util.Evaluable;

public class FunctionEntry implements Comparable<FunctionEntry>, Evaluable {
    private String name = "";
    private String[] parameters = new String[0];
    private Expression expression = null;

    public FunctionEntry(String name, String[] parameters, Expression exp) {
	this.name = name;
	if (null != parameters)
	    this.parameters = parameters;
	this.expression = exp;
    }

    public String getName() {
	return this.name;
    }

    public int getParameterCount() {
	return this.parameters.length;
    }

    /**
     * �������ܿ��ǣ����ﷵ�ص����β����鱾�������Ŀ���
     * <b>���ⲿʹ�÷��ص�����ʱ����Բ�Ӧ���޸�������</b>
     * 
     * @return
     */
    public String[] getParameters() {
	return this.parameters;
	// return Arrays.copyOf( parameters, parameters.length );
    }

    public void setParameters(String[] parameters) {
	this.parameters = parameters;
    }

    public Expression getExpression() {
	return expression;
    }

    public void setExpression(Expression expression) {
	this.expression = expression;
    }

    public double getValue(SymbolTable symtable) throws EvaluationException {
	return this.getExpression().getValue(symtable);
    }

    public int compareTo(FunctionEntry o) {
	if (null == o)
	    return 1;
	if (this == o)
	    return 0;
	int nameCompare = this.name.compareTo(o.name);
	if (0 != nameCompare)
	    return nameCompare;
	return this.parameters.length - o.parameters.length;
    }

    public String toString() {
	StringBuilder b = new StringBuilder();
	b.append(name).append('(');
	int comma = this.parameters.length - 1;
	for (int i = 0; i < this.parameters.length; i++) {
	    b.append(this.parameters[i]);
	    if (i < comma)
		b.append(',').append(' ');
	}
	b.append(')');
	return b.toString();
    }

    public String toFullString() {
	StringBuilder b = new StringBuilder();
	b.append(this.toString());
	b.append(' ').append('=').append(' ');
	b.append(expression.toString());
	return b.toString();
    }
}
